package com.lwj.liwanjia.view.activity.work.shang_jia.spfabu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.fabu.PhotoViewPagerAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.utils.HackyViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/14.
 */

public class PhotoViewActivity extends BaseActivity {

    @BindView(R.id.photo_vp)
    HackyViewPager photoVp;
    @BindView(R.id.current_position)
    TextView currentPosition;

    private List<Fragment> list = new ArrayList<>();
    private PhotoViewPagerAdapter pagerAdapter;
    private List<String> imageUrl = new ArrayList<>();
    private int currentItem = 0;

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            currentItem = bundle.getInt("position");
            ArrayList<String> igList = bundle.getStringArrayList("imageUrl");
            if (igList != null) {
                imageUrl.addAll(igList);
            }
            String url = bundle.getString("url");
            if (url != null) {
                imageUrl.add(url);
            }
        }
        for (int i = 0; i < imageUrl.size(); i++) {
            list.add(PhotoViewFragment.getInstance(imageUrl.get(i)));
        }
        if (imageUrl.size() > 1) {
            currentPosition.setText((currentItem + 1) + "/" + imageUrl.size());
        } else {
            currentPosition.setVisibility(View.INVISIBLE);
        }
        pagerAdapter = new PhotoViewPagerAdapter(getSupportFragmentManager(), list);
        photoVp.setAdapter(pagerAdapter);
        photoVp.setCurrentItem(currentItem);
        photoVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition.setText((position + 1) + "/" + imageUrl.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photoview;
    }

}
